"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountDao = void 0;
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
const GameConfig_1 = require("../../src/config/GameConfig");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const AccountDailyLogin_1 = require("./AccountDailyLogin");
const AccountDailyRegister_1 = require("./AccountDailyRegister");
const AccountEntity_1 = require("../entity/AccountEntity");
const DBSourceManager_1 = require("../../src/engine/utils/DBSourceManager");
class AccountDao extends DaoBase_1.DaoBase {
    static getRepository() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepositoryBase(AccountEntity_1.AccountEntity, this.getAccountDbName());
            return repository;
        });
    }
    static getAccountNum() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let count = yield repository.count();
            return count;
        });
    }
    static get_account_list(pagenum, pagesize) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let start = (pagenum - 1) * pagesize;
            let sql = "select * from  " + this.Tb_Name + " limit ?,?";
            let sqlParams = [start, pagesize];
            let results = yield DBSourceManager_1.DBSourceManager.instance.query(repository, sql, sqlParams);
            return results;
        });
    }
    static search_account(searchKey, searchValue) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let sql = "select * from  " + this.Tb_Name + "  where " + searchKey + " = ?";
            let sqlParams = [searchValue];
            let results = yield DBSourceManager_1.DBSourceManager.instance.query(repository, sql, sqlParams);
            return results;
        });
    }
    static is_ip_exist(ip) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findAndCountBy({
                ip: ip,
                account_type: GameConfig_1.GameConfig.Account_Type.Account
            });
            if (result[1] >= 1) {
                return true;
            }
            return false;
        });
    }
    static get_account_info(account) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = yield repository.findOneBy({ account: account });
            return result;
        });
    }
    static update_account(entity) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let result = repository.save(entity);
            return result;
        });
    }
    static checkAddDailyLogin(date) {
        return __awaiter(this, void 0, void 0, function* () {
            let startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
            let endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
            let startTime = DateUtil_1.default.formatDateStr(startDate.getTime());
            let endTime = DateUtil_1.default.formatDateStr(endDate.getTime());
            let repository = yield this.getRepository();
            let sql = "select account from " + this.Tb_Name + " WHERE last_login_time >= ? and last_login_time < ?  ";
            let sqlParams = [startTime, endTime];
            let results = yield repository.query(sql, sqlParams);
            // Logger.log("checkAddDailyLogin=", sql, sqlParams,  results)
            let loginDate = DateUtil_1.default.formatDateStr3(date.getTime());
            for (let i = 0; i < results.length; i++) {
                AccountDailyLogin_1.AccountDailyLogin.insertAccountDailyLogin(loginDate, results[i]["account"]);
            }
        });
    }
    static checkAddDailyRegister(date) {
        return __awaiter(this, void 0, void 0, function* () {
            let startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
            let endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
            let startTime = DateUtil_1.default.formatDateStr(startDate.getTime());
            let endTime = DateUtil_1.default.formatDateStr(endDate.getTime());
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getAccountDbName());
            let sql = "select account from " + this.Tb_Name + " WHERE create_time >= ? and create_time < ?  ";
            let sqlParams = [startTime, endTime];
            let results = yield client.queryPromise(sql, sqlParams);
            // Logger.log("checkAddDailyLogin=", sql, sqlParams,  results)
            let registerDate = DateUtil_1.default.formatDateStr3(date.getTime());
            for (let i = 0; i < results.length; i++) {
                AccountDailyRegister_1.AccountDailyRegister.insertAccountDailyRegister(registerDate, results[i]["account"]);
            }
        });
    }
}
exports.AccountDao = AccountDao;
AccountDao.Tb_Name = "t_accounts";
